package com.backend.system.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页bean
 *
 * @author dormitory
 */
@Data
@ApiModel("分页bean")
public class PageBean<T> {
    /**
     * 数据列表
     */
    @ApiModelProperty(name = "data", value = "数据列表")
    private List<T> data;

    /**
     * 总数据行数
     */
    @ApiModelProperty(name = "total", value = "总数据行数")
    private Integer total;

    /**
     * 页数
     */
    @ApiModelProperty(name = "pages", value = "页数")
    private Integer pages;

    /**
     * 每页大小
     */
    @ApiModelProperty(name = "pageSize", value = "每页大小")
    private Integer pageSize;

    /**
     * 当前页
     */
    @ApiModelProperty(name = "pageNum", value = "当前页")
    private Integer pageNum;

    public PageBean() {
        this.data = new ArrayList<>(0);
        this.total = 0;
        this.pages = 0;
        this.pageNum = 1;
        this.pageSize = 0;
    }

    public PageBean(List<T> data, Integer total, Integer page, Integer pageSize) {
        this.data = (null == data ? new ArrayList<T>(0) : data);
        this.total = total;
        this.pageNum = page;
        this.pageSize = pageSize;
        this.pages = (int)Math.ceil((double)total / (double)pageSize);
    }

    public PageBean(Integer total, Integer pages, Integer pageSize, Integer pageNum) {
        this.total = total;
        this.pages = pages;
        this.pageSize = pageSize;
        this.pageNum = pageNum;
    }
}
